const BASE_URL = 'https://www.ztycangchu.com/wexin-api'

export const request = (option) => {
	const token = uni.getStorageSync('token');
	if (token) {
		option.header = {
			'Authorization': 'Bearer ' + token,
		};
	} else {
		if (option.url.indexOf("/yujiaowang/wechat/") !== 0) {
			uni.navigateTo({
				url: '/pages/login/index'
			});
		}
	}

	uni.showLoading({
		title: "加载中",
		mask: true,
	});

	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + option.url,
			method: option.method || 'GET',
			data: option.data || {},
			header: option.header,
			timeout: 5000,
			success: (res) => {
				uni.hideLoading();
				if (res.statusCode !== 200) {
					if (res.statusCode == 500) {
						return uni.showToast({
							title: '服务器重启中...',
							icon: "none",
							mask: true,
						})
					} else {
						return uni.showToast({
							title: '获取数据失败',
							icon: "none",
							mask: true,
						})
					}
				} else {
					resolve(res.data)
				}
			},
			fail: (err) => {
				uni.hideLoading();
				uni.showToast({
					title: '请求接口失败',
					icon: "none",
					mask: true,
				})
				reject(err)
			}
		})
	})
}
